package com.roger.myHome.util;

import com.alibaba.fastjson.JSONArray;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class BaseUtil {

    public static String success(){
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("success", "true");
        return JSONArray.toJSONString(resultMap);
    }

    public static String fail(){
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("success", "false");
        return JSONArray.toJSONString(resultMap);
    }

    public static String validate(Object o){
        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        Validator validator = vf.getValidator();
        Set<ConstraintViolation<Object>> set = validator.validate(o);
        String validateResult = "";
        for (ConstraintViolation<Object> constraintViolation : set) {
            validateResult = constraintViolation.getPropertyPath() + ":" + constraintViolation.getMessage();
            //log.error(validateResult);
            break;
        }
        return validateResult;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            int ra = (int) ((Math.random() * 9 + 1) * -1000000);
            //System.out.println(ra);
        }
// 获取今天的日期
        LocalDate today = LocalDate.now();
        // 计算昨天的日期
        LocalDate yesterday = today.minusDays(1);
        // 打印昨天的日期
        //System.out.println(yesterday.toString());
//        System.out.println();
//        System.out.println(System.currentTimeMillis());
    }

}
